Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

334
Visualizações
What is the difference between [...arr, addThisElement], and arr.push(addThisElement) when using useState in ReactJS?

Could someone please explain the difference between this:

const arr = users;

arr.push({ firstName, email })

setUsers((prevState) => arr)

and this:

setUsers(prevState => [...prevState, { firstName, email }])
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The answer is references and object equality...

Array.prototype.push() mutates the array in place meaning its object reference does not change.

const prevState = [1,2,3];
const nextState = prevState;
nextState.push(4);
nextState === prevState; // true

[...prevState, { firstName, email }] creates a new array which is not equal to prevState.

const prevState = [1,2,3];
const nextState = [...prevState, 4];
nextState === prevState; // false

As per React's state change detection rules...

Bailing out of a dispatch

If you return the same value from a Reducer Hook as the current state, React will bail out without rendering the children or firing effects. (React uses the Object.is comparison algorithm.)

Using .push() and updating the state to the same value means React will bail out of the re-render and you won't see changes made.

users.push({
  firstName: "Bob",
  email: "bob@example.com"
});
setUsers(users);

Edit lucid-williamson-21sejq

Create a new array and the changes are made visible

setUsers((prev) => [
  ...prev,
  {
    firstName: "Bob",
    email: "bob@example.com"
  }
]);

Edit keen-napier-hk5m5i

about 4 years ago · Juan Pablo Isaza Relatório

0

In the first case you mutate the reference arr adding a user. In the second case you create a completely new array copying all the data from the old one plus adding the new user.

You should use the second case as react compares the state using Object.is, which in case of your first case would return true as the reference has not changed even if added a new user and therefore ui would not be updated.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda